package com.sun.study;

import com.sun.study.utils.jdbcUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

/**
 * @Author Sun
 * @Date 2020/4/11 23:22
 * @Version 1.0
 */
public class TestPrepareStatement {
    public static void main(String[] args) {

        Connection connection = null;
        PreparedStatement preparedStatement = null;
        ResultSet rs = null;

        try {
             connection = jdbcUtils.getConnection();//获取数据库连接

            String sql = "delete from course where CNO=?";//使用? 占位符 代表参数

            preparedStatement = connection.prepareStatement(sql);//预编译SQL，先写SQL，但不执行

            preparedStatement.setString(1, "C06");//手动给参数按顺序赋值，下标从1开始

            int i = preparedStatement.executeUpdate();//执行SQL，不用把sql传入

            if(i>0){
                System.out.println("删除成功");
            }


        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            jdbcUtils.Release(connection, preparedStatement, rs);
        }

    }
}
